package com.zy.edu.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zy.edu.bean.*;
import com.zy.edu.config.SystemParam;
import com.zy.edu.dao.CourseChapterHomeworkStuDao;
import com.zy.edu.service.CourseChapterHomeworkStuService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zy.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HuangHaiShui
 * @since 2021-08-26
 */
@Service
@Transactional
public class CourseChapterHomeworkStuServiceImpl extends ServiceImpl<CourseChapterHomeworkStuDao, CourseChapterHomeworkStu> implements CourseChapterHomeworkStuService {

    @Resource
    UserService userService;

    @Override
    public void updateAllStatus(String id) {
        this.baseMapper.updateAllStatus( id);
    }

    @Override
    public Map submit(CourseChapterHomeworkStu courseChapterHomeworkStu, InputParam inputParam) {

        if(!StringUtils.isEmpty(courseChapterHomeworkStu.getId())){
            CourseChapterHomeworkStu temp = new CourseChapterHomeworkStu();
            temp.setId(courseChapterHomeworkStu.getId());
            temp.setUrl(courseChapterHomeworkStu.getUrl());
            this.updateById(temp);
            return ResponseInfo.success(null);
        }
        return ResponseInfo.error("作业上传失败");
    }

    @Override
    public Map correct(CourseChapterHomeworkStu courseChapterHomeworkStu, InputParam inputParam) {

        User currentUser = userService.selectById(inputParam.getUserId());
        CourseChapterHomeworkStu temp = new CourseChapterHomeworkStu();
        temp.setId(courseChapterHomeworkStu.getId());
        temp.setScore(courseChapterHomeworkStu.getScore());
        temp.setAbilityAutonomy(courseChapterHomeworkStu.getAbilityAutonomy());
        temp.setAbilityCreate(courseChapterHomeworkStu.getAbilityCreate());
        temp.setAbilityMind(courseChapterHomeworkStu.getAbilityMind());
        temp.setAbilityOpera(courseChapterHomeworkStu.getAbilityOpera());
        temp.setAbilitySelf(courseChapterHomeworkStu.getAbilitySelf());
        if(currentUser.getRoleType().equals(Role.TEACHER) ){
            this.updateById(temp);
            return ResponseInfo.success(null);
        }else{
            return ResponseInfo.error("仅教师才可批改作业");
        }
    }

    @Override
    public Map getByStuId(InputParam inputParam) {

        //查分页
        if(inputParam!=null && inputParam.getPage()!=null && inputParam.getSize()!=null
                && inputParam.getPage()>0 &&  inputParam.getSize()>0
                && !StringUtils.isEmpty(inputParam.getUserId())){
            Page<CourseChapterHomeworkStu> page = new Page<CourseChapterHomeworkStu>(inputParam.getPage(), inputParam.getSize());
            List<CourseChapterHomeworkStu> tempList = this.baseMapper.getByStuId(page,inputParam);
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("rows", tempList);
            resultMap.put("total", page.getTotal());
            return ResponseInfo.success(resultMap);
        }else {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }


    }

    @Override
    public Map getByClsId(CourseChapterHomeworkStu courseChapterHomeworkStu, InputParam inputParam) {
        if(inputParam!=null && inputParam.getPage()!=null && inputParam.getSize()!=null
                && inputParam.getPage()>0 &&  inputParam.getSize()>0
                ){
            Page<CourseChapterHomeworkStu> page = new Page<CourseChapterHomeworkStu>(inputParam.getPage(), inputParam.getSize());
            List<CourseChapterHomeworkStu> tempList = this.baseMapper.getByClsId(page,inputParam);
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("rows", tempList);
            resultMap.put("total", page.getTotal());
            return ResponseInfo.success(resultMap);
        }else {
            return ResponseInfo.error(SystemParam.ERROR_PARAMS);
        }
    }

    @Override
    public int countByClsId(String clsId, boolean flag,String hkId,boolean score) {

        EntityWrapper<CourseChapterHomeworkStu> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("class_id",clsId);
        if(!StringUtils.isEmpty(hkId)){
            entityWrapper.eq("hk_id",hkId);
        }
        if(flag){//统计已提交的作业
            entityWrapper.andNew("url is not null ");
        }
        if(score){//统计已评分的作业
            entityWrapper.andNew("score is not null ");
        }
        int count = this.selectCount(entityWrapper);
        return count;
    }

    @Override
    public void deleteByHkId(String id,String clsId) {
        this.baseMapper.deleteByHkId( id,clsId);
    }

    @Override
    public StatsAbility getAbility(String id) {
        if(!StringUtils.isEmpty(id)){
           return this.baseMapper.getAbility( id);
        }
        return null;
    }
}
